﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;

//模拟帧 FE 26 4B 81 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 31 32 33 34 35 36 37 38 01 02 77 88 99 01 02 44 03 04 55 05 06 66 38 C9
namespace ParkingSystem.RxPacket
{
    public struct SensorHeartBeat
    {
        public ulong dcIEEEAddress { get; set; }         // field
        public byte clusterID { get; set; }              // field
        public ushort sensorShortAddress { get; set; }   // field
        public uint firmwareVersion { get; set; }        // field
        public ulong sensorIEEEAddress { get; set; }     // field
        public ushort fartherShortAddress { get; set; }  // field
        public byte rssi { get; set; }                   // field
        public byte voltage { get; set; }                // field
        public byte detectStatus { get; set; }           // field
        public ushort neighbor1 { get; set; }            // field
        public byte lqi1 { get; set; }                   // field
        public ushort neighbor2 { get; set; }            // field
        public byte lqi2 { get; set; }                   // field
        public ushort neighbor3 { get; set; }            // field
        public byte lqi3 { get; set; }                   // field
        public byte seqNumb { get; set; }                // field

        private const byte _size = 38;                   // 包的字节大小
        public static byte size
        {
            get { return _size; }
        }

        private const byte _dcIEEEAddressPos = 0;        // 字段所处起始位置
        public static byte dcIEEEAddressPos
        {
            get { return _dcIEEEAddressPos; }
        }

        private const byte _clusterIDPos = 8;            // 字段所处起始位置
        public static byte clusterIDPos
        {
            get { return _clusterIDPos; }
        }

        private const byte _sensorShortAddressPos = 9;   // 字段所处起始位置
        public static byte sensorShortAddressPos
        {
            get { return _sensorShortAddressPos; }
        }

        private const byte _firmwareVersionPos = 11;     // 字段所处起始位置
        public static byte firmwareVersionPos
        {
            get { return _firmwareVersionPos; }
        }

        private const byte _sensorIEEEAddressPos = 15;   // 字段所处起始位置
        public static byte sensorIEEEAddressPos
        {
            get { return _sensorIEEEAddressPos; }
        }

        private const byte _fartherShortAddressPos = 23; // 字段所处起始位置
        public static byte fartherShortAddressPos
        {
            get { return _fartherShortAddressPos; }
        }

        private const byte _rssiPos = 25;                // 字段所处起始位置
        public static byte rssiPos
        {
            get { return _rssiPos; }
        }

        private const byte _voltagePos = 26;             // 字段所处起始位置
        public static byte voltagePos
        {
            get { return _voltagePos; }
        }

        private const byte _detectStatusPos = 27;        // 字段所处起始位置
        public static byte detectStatusPos
        {
            get { return _detectStatusPos; }
        }

        private const byte _neighbor1Pos = 28;           // 字段所处起始位置
        public static byte neighbor1Pos
        {
            get { return _neighbor1Pos; }
        }

        private const byte _lqi1Pos = 30;                 // 字段所处起始位置
        public static byte lqi1Pos
        {
            get { return _lqi1Pos; }
        }

        private const byte _neighbor2Pos = 31;            // 字段所处起始位置
        public static byte neighbor2Pos
        {
            get { return _neighbor2Pos; }
        }

        private const byte _lqi2Pos = 33;                 // 字段所处起始位置
        public static byte lqi2Pos
        {
            get { return _lqi2Pos; }
        }

        private const byte _neighbor3Pos = 34;            // 字段所处起始位置
        public static byte neighbor3Pos
        {
            get { return _neighbor3Pos; }
        }

        private const byte _lqi3Pos = 36;                 // 字段所处起始位置
        public static byte lqi3Pos
        {
            get { return _lqi3Pos; }
        }

        private const byte _seqNumbPos = 37;              // 字段所处起始位置
        public static byte seqNumbPos
        {
            get { return _seqNumbPos; }
        }

        public const ushort Type = (ushort)0x814B;         // 表示数据类型

        public void Parse(List<byte> rxPacket, int index)  // 对数据进行解析, rxPacket表示原始数据帧,index则表示从哪个地址开始解析
        {
            byte[] conver = new byte[8];
            for (int i = 0; i < 8; i++)
                conver[i] = rxPacket[index + dcIEEEAddressPos + i];

            this.dcIEEEAddress = BitConverter.ToUInt64(conver, 0);

            this.clusterID = rxPacket[index + clusterIDPos];

            for (int i = 0; i < 2; i++)
                conver[i] = rxPacket[index + sensorShortAddressPos + i];

            this.sensorShortAddress = BitConverter.ToUInt16(conver, 0);

            for (int i = 0; i < 4; i++)
                conver[i] = rxPacket[index + firmwareVersionPos + i];

            this.firmwareVersion = BitConverter.ToUInt32(conver, 0);

            for (int i = 0; i < 8; i++)
                conver[i] = rxPacket[index + sensorIEEEAddressPos + i];

            this.sensorIEEEAddress = BitConverter.ToUInt64(conver, 0);

            for (int i = 0; i < 2; i++)
                conver[i] = rxPacket[index + fartherShortAddressPos + i];

            this.fartherShortAddress = BitConverter.ToUInt16(conver, 0);

            this.rssi = rxPacket[index + rssiPos];

            this.voltage = rxPacket[index + voltagePos];

            this.detectStatus = rxPacket[index + detectStatusPos];

            for (int i = 0; i < 2; i++)
                conver[i] = rxPacket[index + neighbor1Pos + i];
            this.neighbor1 = BitConverter.ToUInt16(conver, 0);

            this.lqi1 = rxPacket[index + lqi1Pos];

            for (int i = 0; i < 2; i++)
                conver[i] = rxPacket[index + neighbor2Pos + i];
            this.neighbor2 = BitConverter.ToUInt16(conver, 0);

            this.lqi2 = rxPacket[index + lqi2Pos];

            for (int i = 0; i < 2; i++)
                conver[i] = rxPacket[index + neighbor3Pos + i];
            this.neighbor3 = BitConverter.ToUInt16(conver, 0);

            this.lqi3 = rxPacket[index + lqi3Pos];

            this.seqNumb = rxPacket[index + seqNumbPos];
        }

        public override string ToString()
        {
            string s = string.Format("{0,-25} {1,20} {2,15}\n", "(Field)", "(Hex)", "(Decimal)");
            s += string.Format("{0,-25}|{1,20:X4} {2,15:G}\n", "Type", Type, this.GetType().Name);
            s += string.Format("{0,-25}|{1,20:X16}\n", "DCIEEEAddress", this.dcIEEEAddress);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "ClusterID", this.clusterID);
            s += string.Format("{0,-25}|{1,20:X4}\n", "SensorShortAddress", this.sensorShortAddress);
            s += string.Format("{0,-25}|{1,20:X8} {1,15:G}\n", "FirmwareVersion", this.firmwareVersion);
            s += string.Format("{0,-25}|{1,20:X16}\n", "SensorIEEEAddress", this.sensorIEEEAddress);
            s += string.Format("{0,-25}|{1,20:X4}\n", "FartherShortAddress", this.fartherShortAddress);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "rssi", this.rssi);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "Voltage", this.voltage);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "DetectStatus", this.detectStatus);
            s += string.Format("{0,-25}|{1,20:X4}\n", "Neighbor1", this.neighbor1);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "Lqi1", this.lqi1);
            s += string.Format("{0,-25}|{1,20:X4}\n", "Neighbor2", this.neighbor2);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "Lqi2", this.lqi2);
            s += string.Format("{0,-25}|{1,20:X4}\n", "Neighbor3", this.neighbor3);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}\n", "Lqi3", this.lqi3);
            s += string.Format("{0,-25}|{1,20:X2} {1,15:G}", "SeqNum", this.seqNumb);


            return s;
        }
    }
}